<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace web\api\controller;

use addons\video\model\VideoLike;
use addons\video\model\Video as VideoM;
use addons\video\model\VideoTags;
use addons\video\model\VideoActor;

/**
 * Class SmallVideo
 * @package web\api\controller
 */
class SmallVideo extends UserBase{

    /**
     * 标签列表
     */
    public function tagsList(){
        try{
            $tagsM = new VideoTags;
            $list = $tagsM->getDataList(-1,-1,' and is_on=1 ', '*','order_index asc');
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 发布视频
     */
    public function addVideo(){
        try{
            $videoM = new VideoM;
            $data['user_id'] = $this->user_id;
            $data['video_title'] = $this->getParamVal('video_title');
            $data['video_length'] = $this->_post('video_length');
            $data['video_path'] = $this->getParamVal('video_path');
            $data['video_img'] = $data['video_path'].'?x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast';
            $data['tags'] = $this->getParamVal('tags');
            $data['is_public'] = 1;
            $data['status'] = 0;
            $data['video_type'] = 2;
            $data['create_time'] = NOW_DATETIME;
            $data['update_time'] = NOW_DATETIME;
            $res = $videoM->add($data);
            if($res) return $this->successJSON();
            else return $this->failJSON('上传视频失败');
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 上传视频列表
     */
    public function videoUploadList(){
        try{
            $videoM = new VideoM;
//            $tagsM = new VideoTags;
            $status = $this->_post('status');
            $filter = ' a.user_id= '.$this->user_id.' and video_type = 2 ';
            if($status != ''){
                $filter .= ' and status = '.$status;
            }

            $list = $videoM->getUploadVideoList($this->getPageIndex(),$this->getPageSize(), $filter, 'a.id,a.video_title,a.video_length,a.video_path,a.video_img,a.tags,a.status,a.create_time,a.update_time,a.video_view', 'a.id desc');
//            if($list){
//                foreach ($list as &$value){
//                    $value['tags'] = explode(",", $value['tags']);
//            foreach ($value['tags'] as $key => &$val){
//
//                $val[$key] = $tagsM->where(['id' => $val])->value('tags_name')??'';
////                        dump($val[$key]);die;
//            }
//                }
//            }
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 视频详情
     */
    public function videoUploadDetails(){
        try{
            $videoM = new VideoM;
            $tagsM = new VideoTags;
            $id = $this->getParamVal('id');
            $video_data = $videoM->getDetail($id);
            if(empty($video_data)){
                return $this->failJSON('视频不存在');
            }

            $video_data['tags'] = explode(",", $video_data['tags']);
            $video_data['tags_arr'] = [];
            $tags_arr = [];
            foreach ($video_data['tags'] as $key => &$val){
                $tags_arr[$key]['id'] = $val;
                $tags_arr[$key]['name'] = $tagsM->where(['id' => $val])->value('tags_name')??'';
            }
            $video_data['tags_arr'] = $tags_arr;
            return $this->successJSON($video_data);

        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 删除视频
     */
    public function videoUploadDel(){
        try{
            $videoM = new VideoM;
            $id = $this->getParamVal('id');
            $video_data = $videoM->where(['user_id' => $this->user_id, 'id' => $id])->find();
            if(empty($video_data)){
                return $this->failJSON('视频不存在');
            }
            $re = $videoM->where(['id' => $id])->delete();
            if(!$re){
                return $this->failJSON('删除失败');
            }
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 视频解锁列表
     */
    public function unlockList(){
        try{
            $video_type = $this->getParamVal('video_type');
            $videoPayM = new \addons\video\model\VideoPay();
            $filter = ' a.user_id = '. $this->user_id . ' and b.video_type = '.$video_type;
            $list = $videoPayM->getVideoPayList($this->getPageIndex(),$this->getPageSize(), $filter, 'a.id,a.user_id,a.video_id,a.price,a.create_time,a.end_time,b.video_title,b.video_length,b.video_path,b.video_img,b.tags,b.status,b.create_time,b.praise_number,b.update_time,b.video_view,m.head_img', 'a.id desc');
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }


}
